/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.hibernate.dao;

import com.cashmate.hibernate.pojo.Areas;
import java.util.HashMap;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;

/**
 *
 * @author Dean
 */
public class AreaDAO extends DAO {

    public AreaDAO() {
        super();
    }

    //BASIC DAO METHODS...
    @SuppressWarnings("unchecked")
    public List<Areas> getAll() throws Exception {
        List<Areas> a = (List<Areas>) getSession().createQuery("from Areas a").setCacheable(true).list();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public Areas get(int id) throws Exception {
        Areas a = (Areas) getSession().createQuery("from Areas a where a.areaid=:id").setParameter("id", id).setCacheable(true).uniqueResult();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public void update(Areas a) throws Exception {
        try {
            begin();
            getSession().update(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void insert(Areas a) throws Exception {
        try {
            begin();
            getSession().save(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void delete(int id) throws Exception {
        try {
            begin();
            getSession().delete(get(id));
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public List<Areas> findByCriteria(DetachedCriteria dc, int from, int size) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setFirstResult(from);
        criteria.setMaxResults(size);
        criteria.setCacheable(true);
        List<Areas> list = criteria.list();
        flush();
        return list;
    }

    @SuppressWarnings("unchecked")
    public int countByCriteria(DetachedCriteria dc) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setCacheable(true);
        criteria.setProjection(Projections.rowCount());
        int count = ((Integer) criteria.list().get(0)).intValue();
        flush();
        return count;
    }

    //Other Methods...
    @SuppressWarnings("unchecked")
    public Areas getByAreaCode(String areacode) throws Exception {
        Areas a = (Areas) getSession().createQuery("from Areas a where a.areacode=:code").setParameter("code", areacode).setMaxResults(1).setCacheable(true).uniqueResult();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public List<Areas> getAreasPerBranch(int bid) throws Exception {
        List<Areas> areas = (List<Areas>) getSession().createQuery("from Areas a where a.branches.branchid=:bid").setParameter("bid", bid).setCacheable(true).list();
        flush();
        return areas;
    }

    @SuppressWarnings("unchecked")
    public HashMap<String, Areas> getAreasMap() throws Exception {
        HashMap<String, Areas> map = new HashMap<String, Areas>();
        List<Areas> l = getAll();
        flush();
        for (Areas u : l) {
            map.put(u.getAreacode(), u);
        }
        return map;
    }

    @SuppressWarnings("unchecked")
    public boolean hasDuplicate(String areacode) throws Exception {
        Areas e = (Areas) getSession().createQuery("from Areas a where a.areacode=:code").setParameter("code", areacode).setCacheable(true).uniqueResult();
        flush();
        if (e == null) {
            return false;
        }
        return true;
    }
}
